package com.sxkf.template.mq;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class QueueConfig {

    // 队列
    public static final String Q_DELAYED = "dq.delayed";

    // 交换机
    public static final String EX_DELAYED = "dex.delayed";

    // 路由key
    public static final String R_DELAYED = "dr.delayed";


    // 延迟交换机
    @Bean("delayedExchange")
    public CustomExchange delayedExchange(){
        Map<String,Object> args = new HashMap<>(1);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(EX_DELAYED,"x-delayed-message",true,false,args);
    }

    // 声明队列
    @Bean("delayedQueue")
    public Queue delayedQueue(){
        return new Queue(Q_DELAYED);
    }


    // 队列与交换机进行绑定
    public void bindQueueExchange(@Qualifier("delayedQueue") Queue queue, @Qualifier("delayedExchange") Exchange exchange){
        BindingBuilder.bind(queue).to(exchange).with(R_DELAYED).noargs();
    }

}
